package battleship;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;

/**
 *
 * @author Kamil
 */
@SuppressWarnings("serial")
public class Grid extends JPanel implements Iterable<Field> {

	public Field fields[][];
	public int rows, cols;

	public Grid(int row, int col) {
		super();
		rows = row;
		cols = col;

		setLayout(new GridLayout(rows + 1, cols + 1));

		Field fTemp = new Field(-1, -1);
		fTemp.setToolTipText("");
		fTemp.changeType(FieldType.INACTIVE);
		add(fTemp);

		for (int i = 0; i < cols; i++) {
			fTemp = new Field(-1, i);
			fTemp.setToolTipText("" + (i + 1));
			fTemp.setMyLabel("" + (i + 1));
			fTemp.changeType(FieldType.INACTIVE);
			add(fTemp);
		}

		fields = new Field[rows][cols];
		for (int i = 0; i < rows; i++) {
			fTemp = new Field(i, -1);
			fTemp.setToolTipText("" + ((char) ('A' + i)));
			fTemp.setMyLabel("" + ((char) ('A' + i)));
			fTemp.changeType(FieldType.INACTIVE);
			add(fTemp);

			for (int j = 0; j < cols; j++) {
				fields[i][j] = new Field(i, j);
				add(fields[i][j]);
			}
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
		Dimension dim = getSize();

		g2d.setColor(Color.BLUE);
		g2d.fillRect(0, 0, dim.width, dim.height);
	}

	public void setButtonsEnabled(boolean enabled) {
		for (Field[] f : fields) {
			for (Field g : f) {
				g.setEnabled(enabled);
			}
		}
	}

	public List<Field> getNeighbours(Field f) {
		if (fields[f.row][f.col] != f) {
			return null;
		}
		ArrayList<Field> list = new ArrayList<Field>();

		if (f.row + 1 < rows) {
			list.add(fields[f.row + 1][f.col]);
		}
		if (f.row > 0) {
			list.add(fields[f.row - 1][f.col]);
		}
		if (f.col + 1 < cols) {
			list.add(fields[f.row][f.col + 1]);
		}
		if (f.col > 0) {
			list.add(fields[f.row][f.col - 1]);
		}
		return list;
	}

	public List<Field> getNeighboursWithCorners(Field f) {
		if (fields[f.row][f.col] != f) {
			return null;
		}
		ArrayList<Field> list = new ArrayList<Field>();

		if (f.row + 1 < rows) {
			if (f.col > 0) {
				list.add(fields[f.row + 1][f.col - 1]);
			}
			list.add(fields[f.row + 1][f.col]);
			if (f.col + 1 < cols) {
				list.add(fields[f.row + 1][f.col + 1]);
			}
		}
		if (f.row > 0) {
			if (f.col > 0) {
				list.add(fields[f.row - 1][f.col - 1]);
			}
			list.add(fields[f.row - 1][f.col]);
			if (f.col + 1 < cols) {
				list.add(fields[f.row - 1][f.col + 1]);
			}
		}
		if (f.col > 0) {
			list.add(fields[f.row][f.col - 1]);
		}
		if (f.col + 1 < cols) {
			list.add(fields[f.row][f.col + 1]);
		}

		return list;
	}

	@Override
	public void addKeyListener(KeyListener keyAdapter) {
		super.addKeyListener(keyAdapter);

		for (Field f : this) {
			f.addKeyListener(keyAdapter);
		}
	}

	@Override
	public Iterator<Field> iterator() {
		return new Iterator<Field>() {

			int r = 0;
			int c = -1;

			@Override
			public boolean hasNext() {
				++c;
				if (c == cols) {
					++r;
					c = 0;
				}
				return c < cols && r < rows;
			}

			@Override
			public Field next() {
				return fields[r][c];
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException("Not supported yet.");
			}
		};
	}

	void setFieldsType(FieldType fieldType) {
		for (Field f : this) {
			f.fieldType = fieldType;
		}
	}
}
